package com.uh.calculation.util;

import com.uh.microservice.core.dao.JdbcDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.expression.spel.ast.NullLiteral;
import scala.annotation.meta.param;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class GetOrganization {
    private JdbcDao jdbcDao;


    public GetOrganization(JdbcDao jdbcDao) {
        this.jdbcDao = jdbcDao;
    }


    public List<Map<String, Object>> getFieldName(String tabName, Integer year, String surveyobjectid) {
        List<Map<String, Object>> list = null;
        String name = "";
        try {
            //String sql = "select  * from  indi_dic  where  name_ch = '组织机构代码' and  view_name = '%s' and  year = '2021'";
            String q = "";
            //List<Map<String, Object>> ls = jdbcDao.loadList(q);
            //if (ls.size() > 0) {
            //name = ls.get(0).get("column").toString();
            //if (!StringUtils.isEmpty(name)) {
            String tb = "`" + tabName + "`";
            q = "select  * from  %s  where  company_code = '%s'  ";
            q = String.format(q, tb, surveyobjectid);
            list = jdbcDao.loadList(q);
            //}
            // }
        } catch (Exception e) {

        }
        return list;
    }

    public List<Map<String, Object>> getBatchFieldName(String tabName, int  st) {
        List<Map<String, Object>> list = null;
        String name = "";
        try {
            String q = "";

            String tb = "`" + tabName + "`";
            q = "select e.company_code,et.*,et203.*,et104.* from  %s  e" +
                    "    left   join  " +
                    "    (select group_concat(json_object(" +
                    "    'M12',M12," +
                    "    'M14',M14," +
                    "    'M26',M26," +
                    "    'M27',M27," +
                    "    'M20',M20," +
                     "   'M21',M21," +
                    "    'company_code',company_code," +
                    "    'time_code',time_code" +
                    "    )) as topicResponsibles ,company_code" +
                    "    from  %s" +
                    "    GROUP BY  company_code" +
                    "    ORDER BY  time_code  asc" +
                    "    ) et  on  e.company_code = et.company_code"+
                    "    left   join" +
                    "    (select group_concat(json_object(" +
                    "    'M26',M26," +
                    "    'time_code',time_code" +
                    "    )) as et203Res ,company_code" +
                    "    from `e203`" +
                    "    GROUP BY  company_code" +
                    "    ORDER BY  time_code  asc" +
                    "    ) et203  on  e.company_code = et.company_code"+
                    "    left   join" +
                    "    (select group_concat(json_object(" +
                    "    'M6',M6," +
                    "    'M11',M11," +
                    "    'time_code',time_code" +
                    "    )) as et104Res ,company_code" +
                    "    from `e104-1`" +
                    "    GROUP BY  company_code" +
                    "    ORDER BY  time_code  asc" +
                    "    ) et104  on  e.company_code = et104.company_code"+

                    "    GROUP BY e.company_code" +
                    "    LIMIT %s,5000";
            q = String.format(q, tb,tb, st);
            list = jdbcDao.loadList(q);






            //}
            //}
        } catch (Exception e) {
            e.printStackTrace();

        }
        return list;
    }


    public String gethc(String surveyobjectid) {
        List<Map<String, Object>> list = null;
        String name = "";
        String year_is_month = "";
        try {
            String sql = "select  * from  supervise_code_company  where  company_code = '%s' and  is_now =1  ORDER BY create_time desc  limit 1 ";
            String q = String.format(sql, surveyobjectid);
            List<Map<String, Object>> ls = jdbcDao.loadList(q);

            if (ls.size() > 0) {
                year_is_month = ls.get(0).get("year_is_month").toString();

            }
        } catch (Exception e) {

        }
        return year_is_month;
    }


    public Integer getcount(String tabname) {
        Integer  num =0;
        try {
            String sql = "select  COUNT(*) as num  from  %s     ";
            String q = String.format(sql, tabname);
            List<Map<String, Object>> ls = jdbcDao.loadList(q);

            if (ls.size() > 0) {
                num = Integer.parseInt(ls.get(0).get("num").toString());

            }
        } catch (Exception e) {

        }
        return num;
    }


    public List<Map<String, Object>>  gethj(String tabName,String surveyobjectid ) {
        List<Map<String, Object>> list = null;
        String name = "";
        String year_is_month = "";
        try {
            String sql = "select  * from (  %s  ) A  ORDER BY EDIT_TIME  ASC ";
            String s = "(select  *  from   %s  where locate('%s', `KEYPROPS`)>0   )";
            String st [] =tabName.split(";");
            String sqlSelect="";
            for(int i=0;i<st.length;i++)  {
                if(i<st.length-1){
                    sqlSelect+= String.format(s, st[i],surveyobjectid);
                    sqlSelect+=" UNION  ";
                }else{
                    sqlSelect+= String.format(s, st[i],surveyobjectid);
                }
            }
            String q = String.format(sql, sqlSelect);
            list= jdbcDao.loadList(q);
        } catch (Exception e) {

        }
        return list;
    }



    /*** 异常--统计人员变动查询
     * */
    public List<Map<String, Object>> getFieldName_TJRY(String tabName, Integer year, String surveyobjectid, Date lastTime) {
        List<Map<String, Object>> list = null;
        String name = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
            String sql = "";//"select  * from  indi_dic  where  name_ch = '组织机构代码' and  view_name = '%s' and  year = '2021'";
            String q = "";//String.format(sql, tabName);
            //List<Map<String, Object>> ls = jdbcDao.loadList(q);
            //if (ls.size() > 0) {
               // name = ls.get(0).get("column").toString();
                //if (!StringUtils.isEmpty(name)) {
                    String tb = "`" + tabName + "`";
                    if (lastTime != null) {
                        String time = simpleDateFormat.format(lastTime);
                        sql = "select  * from  %s  where  company_code = '%s' and time_code <= '%s' order by time_code ASC";
                        q = String.format(sql, tb, surveyobjectid, time);
                    } else {
                        sql = "select  * from  %s  where  company_code = '%s'  order by time_code ASC";
                        q = String.format(sql, tb, surveyobjectid);
                    }
                    list = jdbcDao.loadList(q);
                //}
            //}
        } catch (Exception e) {

        }
        return list;
    }

    public List<Map<String, Object>> getFieldName_206(String tabName, Integer year, String surveyobjectid, Date lastTime) {
        List<Map<String, Object>> list = null;
        String name = "";
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
            String sql = "";//"select  * from  indi_dic  where  name_ch = '组织机构代码' and  view_name = '%s' and  year = '2021'";
            String q = "";//String.format(sql, tabName);
           // List<Map<String, Object>> ls = jdbcDao.loadList(q);
            //if (ls.size() > 0) {
                //name = ls.get(0).get("column").toString();
                //if (!StringUtils.isEmpty(name)) {
                    String tb = "`" + tabName + "`";
                    if (lastTime != null) {
                        String time = simpleDateFormat.format(lastTime);
                        sql = "select  * from  %s  where  company_code = '%s' AND M28 <= '%s' AND M29 <= '%s' order by M30,M28,M29+0 ASC";
                        q = String.format(sql, tb,  surveyobjectid, time.substring(0, 4), time.substring(4, 6));
                    } else {
                        sql = "select  * from  %s  where  company_code = '%s'  order by M30,M28,M29+0 ASC";
                        q = String.format(sql, tb,  surveyobjectid);
                    }
                    list = jdbcDao.loadList(q);
               // }
           // }
        } catch (Exception e) {

        }
        return list;
    }
}
